//
//  PictureBrowserCell.swift
//  WeiBoSwift
//
//  Created by Jacky Sun on 16/5/20.
//  Copyright © 2016年 syf. All rights reserved.
//

import UIKit
import SDWebImage

protocol CellIMgClickDelegate : NSObjectProtocol {
    func CellIMgClick(cell: PictureBrowserCell)
}


class PictureBrowserCell: UICollectionViewCell {
    
     /// 初始化代理
    weak var closeBrowserDelegate: CellIMgClickDelegate?
    
    var url: NSURL?{
        didSet{
            picIMg.sd_setImageWithURL(url) { (image, _, _, _) in
                self.updateSubviewsFrame(image)
            }
            }
    }
    
    /**
     调整子控件位置
     */
    private func updateSubviewsFrame(img: UIImage){
        let newSize  =  returnNewSizeByImage(img)
        
        picIMg.frame = CGRect(origin: CGPointZero, size: newSize)
        //1. 如果是短图的话
        if newSize.height <= UIScreen.mainScreen().bounds.size.height {
            
            let offY = (UIScreen.mainScreen().bounds.size.height - newSize.height) * 0.5
            backScroll.contentInset =  UIEdgeInsets(top: offY, left: 0, bottom: offY, right: 0)
        }
        else{
            backScroll.contentSize = newSize
        }
        
        
    }
    
    
   private func returnNewSizeByImage(img: UIImage) ->CGSize{
        
      let scale = img.size.width / img.size.height
      let  newHeight = UIScreen.mainScreen().bounds.size.width / scale
      return CGSize(width: UIScreen.mainScreen().bounds.size.width, height: newHeight)
    }
    
    
    override init(frame: CGRect) {
       super.init(frame: frame)
        setUpUI()

    }
    
    
    private func setUpUI(){
        contentView.addSubview(backScroll)
        backScroll.addSubview(picIMg)
        picIMg.userInteractionEnabled = true
        picIMg.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(PictureBrowserCell.closeBrowser)))
        backScroll.frame = contentView.bounds
        
        backScroll.delegate = self
        backScroll.minimumZoomScale = 1.0
        backScroll.maximumZoomScale = 1.6
    }
    
     /// 点击图片关闭浏览器
     func closeBrowser(){
       // closeBrowserDelegate?.CellIMgClick(self)
        if let daili = closeBrowserDelegate{
            daili.CellIMgClick(self)
        }
    }
    
    private lazy var backScroll: UIScrollView = UIScrollView()
     lazy var picIMg: UIImageView = UIImageView(frame: CGRectZero)
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension PictureBrowserCell: UIScrollViewDelegate{
    
    func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView?{
        return picIMg
    }
    
    func scrollViewDidEndZooming(scrollView: UIScrollView, withView view: UIView?, atScale scale: CGFloat) {
        
        
        
        var offX = ( UIScreen.mainScreen().bounds.size.width - (view?.frame.size.width)!) * 0.5
        var offY = ( UIScreen.mainScreen().bounds.size.height  - (view?.frame.size.height)!) * 0.5

        offX = offX < 0 ? 0 : offX
        offY = offY < 0 ? 0 : offY
        
        backScroll.contentInset = UIEdgeInsets(top: offY, left: offX, bottom: offY, right: offX)
        
    }
}